<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@taglib prefix="widget" uri="/widget" %>

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/<spring:theme code="stylesheet.wraper" />" />

<div class="wraper">
    <div class="inner-menu">
        <div class="header">
            <span><spring:message code="docrepository.message.title" /></span>
            <div class="prompt"><spring:message code="docrepository.message.prompt.info" /></div>
        </div>
        <div class="list-group dialoguelist">
            <c:forEach items="${messageUsers}" var="entry">
                <c:set var="messageUser" value="${entry.key}" />
                <c:set var="messageCount" value="${entry.value}" />
                <a id="${messageUser.userId}" href="javascript:void(0);" class="list-group-item item">
                    <span class="name"><c:out value="${messageUser.username}" /></span>
                    <span class="badge"><c:out value="${messageCount}" /></span>
                </a>
            </c:forEach>
        </div>
    </div>

    <div class="content">
        <div class="header">
            <span class="contactPrompt">
                <spring:message code="docrepository.message.title.contactPrompt" />
            </span>
            <a class="btn btn-default btn-sm pull-right"><i class="glyphicon glyphicon-plus"></i><spring:message code="docrepository.message.action.sendMessage" /></a>
        </div>
        <div class="dialoguelist">
        </div>
    </div>
</div>
<script style="text/javascript">
var message = message || {
	pageSize: 10, //每页显示的数据
	userMessages: {},  //用户聊天私信
	queryUserMessages: function(oppositeUserId){  //查询用户私信
		if(oppositeUserId){
			// 根据groupId获取群组用户
			$.ajax({
				type: 'POST',
				url: '<widget:resourceUrl mode="/user/messages/listUserMessages" />',
				data: {'oppositeUserId': oppositeUserId, 'pageSize': message.pageSize, 'page': (message.userMessages && message.userMessages[oppositeUserId] || message.userMessages[oppositeUserId].page) || 0},
				success: function(data){
					// 解析数据
					if(data){
						var json = $.parseJSON(data);
						if(json && json.length > 0){
							userFriend.populateUserMessages(json);
							userFriend.userMessages[oppositeUserId].page = (userFriend.userMessages[oppositeUserId].page || 0) + 1;
							userFriend.userMessages[oppositeUserId].data = json;
						}
					}
				}
			});
		}
	},
	populateUserMessages: function(){  //构造用户对话界面
	}
};
$(function(){
	$('.dialoguelist>.item').click(function(){
		var username = $('.name', this).text();
		if(username){
			var contactPrompt = '<spring:message code="docrepository.message.title.contactUserPrompt" />';
			if(contactPrompt){
				$('.contactPrompt', $('.content>.header')).html(contactPrompt.replace('{0}', username));
			}
		}
		var userId = $(this).attr('id');
		if(userId){
			if(!message.userMessages[userId] || !message.userMessages[userId].data || message.userMessages[userId].data.length<=0){
				message.queryUserMessages(userId);
			} else{
				message.populateUserMessages(message.userMessages[userId]);
			}
		}
	});
});
</script>